<?php

/** Zend_View_Helper_Abstract.php */
require_once 'Zend/View/Helper/Abstract.php';

class App_View_Helper_CreateGoogleMarkers extends Zend_View_Helper_Abstract {
    public $labels = array();

    /**
     * Builds google map markers according to places 
     *
     * @param array $places
     * @return string // js code 
     */
    public function CreateGoogleMarkers($places, $type = 'multiple') {        
        $markers = array();
        $this->labels = range('A', 'Z');
        foreach ($places as $key => $place) {  
            $therating = number_format($place['score'], 2);
            $place_name = addslashes($place['name']);
            $address = addslashes($place['address']);
            
            $html = <<<HTML
            {$this->labels[$key]}. \
            <a href="{$this->view->L('place', 'view', array('id' => $place['id']))}">{$place_name}</a><br />\
            {$therating}/5 ({$place['num_reviews']} reviews)<br />\
            {$address}<br />\
            {$place['city']}<br />\
            {$place['phone']}<br />\
            <a href="http://maps.google.com/maps?f=d&hl=en&saddr=&daddr={$address}}&layer=&ie=UTF8&z=10&om=1">Get directions</a><br />
HTML;
            
            $lat = (float)$place['google_latitude'];
            $lon = (float)$place['google_longitude'];
            if ($lat and $lon) {
                $markers[] = <<<MARKER
                var lat = {$lat};
                var lon = {$lon};
                var point_{$key} = new GLatLng(lat||'', lon||'');    
                bounds.extend(point_{$key});
                var coloredIcon = new GIcon(new GIcon(G_DEFAULT_ICON));
                coloredIcon.image = "{$this->getIcon($type, $key)}";
                coloredIcon.shadow = "{$this->getIconShadow($type)}";
                coloredIcon.shadowSize = new GSize(40, 30);
                markerOptions = {
                    icon:coloredIcon
                };
                var marker_{$key} = new GMarker(point_{$key}, markerOptions);
                GEvent.addListener(marker_{$key}, 'click', function() {
                    marker_{$key}.openInfoWindowHtml('{$html}');  
                });  
                map.addOverlay(marker_{$key});
MARKER;
            }
        }
        
        return implode('', $markers);
    }

    public function getIcon($type, $key = false) {
        return $type == 'single' ? $this->view->baseUrl('common/images/blue-dot.png') : "http://www.google.com/mapfiles/marker{$this->labels[$key]}.png";
    }
    
    public function getIconShadow($type) {
        return $this->view->baseUrl('images/mortar-board_shadow.png');
    }
}


